<?php

namespace Api\Controller;

use Vendor\Page;

class GoodsController extends ComController{	



	//商品详情页
	public function getGoodsDetails(){
		$goods_id=I('goods_id','','intval');
		$lat=I('lat');
		$lng=I('lng');
		$store_id=I('store_id','','intval');
		$goods=M('goods')->where("id='$goods_id'")->find();
		$goods['g_img']=explode('|',$goods['g_img']);
		$goods['goods_specification']=unserialize($goods['attr']);

		$storeinfo = M('store')->find($store_id);

        $i = 0;
        while ($i<$storeinfo['store_all_pingfen']){
            $storeinfo['pf'][] = 1;
            $i++;
        }

        $julimi=distance($storeinfo['store_gps_lng'],$storeinfo['store_gps_lat'],$lng,$lat);
        if($julimi<0.1) $list['store_julimi']="<100米";
        else $storeinfo['store_julimi']=number_format(floatval($julimi),2)."km";


        $tags = C('tag');
        $tagsarr_=[];
        if($storeinfo['tags']){
            $tagsarr = explode(',',$storeinfo['tags']);
            foreach ($tagsarr as $k1 => $v1)
            {
                $tagsarr_[] =$tags[$v1];
            }

        }





        $storeinfo['tags'] =$tagsarr_;



		$goods['storeinfo'] = $storeinfo;
		$goods['coupon'] = $this->getCouponsbystoreid($store_id);




		$ajax['code']=1;
		$ajax['info']='ok';
		$ajax['data']=$goods;
		$this->ajaxReturn($ajax);
	}




	public function getRandOrder()
    {

        $rand = C('rand');
        foreach ($rand as $k => $v)
        {
            $rand[$k]['amount'] = mt_rand(1,20);
            $rand[$k]['time'] = mt_rand(1,10).'秒前';
        }
        $join = 'left join '.C('DB_PREFIX').'user as u on u.utoken = o.utoken';
        $map['o.uniacid'] = $this->uniacid;
        $map['o.status'] = 1;
        //$field='u.nickname,u.head';
        ///$data = M('order')->alias('o')->join($join)->where($map)->order('addtime desc')->limit(5)->field($field)->select();
         $field = 'u.nickname,u.head,o.amount';
        $res = M('reward_log')->alias('o')->join($join)->where($map)->order('o.addtime desc')->limit(5)->field($field)->select();
        foreach ($res as $k => $v)
        {
            $res[$k]['time'] = mt_rand(1,10).'秒前';
        }

            $data= $res ?  array_merge($res,$rand):$rand;



        $ajax['code'] = 1;
        $ajax['data'] =$data;
        $this->ajaxReturn($ajax);


    }




    private function getCouponsbystoreid($store_id)
    {






        //商家优惠券
        $juanlist=M('juan')->where("store_id='{$store_id}' and status=1 and num>0")->order('addtime desc')->find();

            $juanlist['end_time']=date('Y.m.d',$juanlist['end_time']);

        return $juanlist;
    }

	//选择规格属性
	public function getOneGoodsAttr(){
		$goods_id=I('goods_id','','intval');
		$data['good_a_info']=M('goods')->field('id,g_name,shop_price,attr')->where("id='$goods_id'")->find();
		$attr=unserialize($data['good_a_info']['attr']);
		$attrs=array();
		foreach($attr['label'] as $k=>$v){
			$attrs[$k]['label']=$v;
			$attrs[$k]['price']=$attr['format_price'][$k];
			$attrs[$k]['format_price']=$attr['format_price'][$k];
			$attrs[$k]['isCheck']=false;
			$attrs[$k]['id']=$k;
		}
		
		$data['good_attr'][0]['attr_type']=1;
		$data['good_attr'][0]['name']='选择属性：';
		$data['good_attr'][0]['values']=$attrs;
		$ajax['code']=1;
		$ajax['info']=$data;
		$this->ajaxReturn($ajax);
	}





	//根据oid获取订单信息
	public function getOrderInfo(){

        $out_trade_no = I('out_trade_no');
        $obj = M('order');
        $obj2 = M('goods');
        $data=[];
        $order = $obj->where(['out_trade_no'=>$out_trade_no])->find();
        if($order['is_pay']!=2){
            $ajax['code']=0;
            $ajax['data']='订单异常';
            $this->ajaxReturn($ajax);
        }
        $goods = $obj2->where(['id'=>$order['goods_id']])->find();
        $tz = M('user')->where(['id'=>$order['uid']])->getField('head');
        $uids = $obj->where(['share_out_trade'=>$out_trade_no])->getField('uid',true);
        $map['id'] = array('in',$uids);
        $zy = M('user')->where($map)->getField('head',true);
        $count =  count($zy);
        $list = array_merge($tz,$zy);
        $info['count'] =$count;
        $info['list'] =$tz;
        $data['goods'] = $goods;
        $data['order'] = $order;
        $data['info'] = $info;
		$ajax['code']=1;
		$ajax['data']=$data;
		$this->ajaxReturn($ajax);
	}

	
    public function getOrderList()
    {

        $p = I('offset',1);
        $status = I('status');
        $is_send = I('is_send');
        $pagesize = 10;
        $where['uniacid'] = $this->uniacid;

        if($status && $status!='undefined'){
            $where['is_pay']  = $status;
        }

        if($is_send && $is_send!='undefined'){
            $where['is_pay']  = 2;
            $where['is_send']  = 1;
        }

        $utoken=I('utoken');
        if(!$utoken){
            $ajax['info']='请先登录';
            $ajax['code']=0;
            $this->ajaxReturn($ajax);
        }

        $uid = M('user')->where(['utoken'=>$utoken])->getField('id');
        $where['uid'] = $uid;


        $data = M('order')->where($where)->page($p.',',$pagesize)->order('addtime desc')->field('id,out_trade_no,is_pay,is_send,is_confirm,goods_id,total_jiner')->select();

        $goodsobj = M('goods');
        foreach ($data as $k => $v)
        {
            $data[$k]['glist'][] = $goodsobj->where(['id'=>$v['goods_id']])->field('id,logo,name')->find();
        }
        $ajax['code'] = 1;
        $ajax['data'] = $data;


        $this->ajaxReturn($ajax);
    }
}
